package Text26;
import java.util.*;
public class Main {
    public static int N = 1010;
    public static  char[] a = new char[N];
    public static char[] b = new char[N];
    public static int[][] f = new int[N][N];
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int m= scanner.nextInt();
        String A = scanner.next();
        String B = scanner.next();
        for (int i = 1; i <=n ; i++) {
            a[i]=A.charAt(i-1);
        }
        for (int i = 1; i <=m ; i++) {
            b[i]=B.charAt(i-1);
        }
        for (int i = 1; i <=n ; i++) {
            for (int j = 1; j <=m ; j++) {
                f[i][j]=Math.max(f[i-1][j],f[i][j-1]);
                if(a[i]==b[j]){
                    f[i][j]=Math.max(f[i][j],f[i-1][j-1]+1);
                }
            }
        }
        System.out.println(f[n][m]);
    }
}
